﻿using UnityEngine;

using System.Collections;
using System.IO;
using UnityEditor;

namespace Example_0000_Atlas_01
{
	

public class AtlasMakerEditor : Editor {
	public override void OnInspectorGUI() {
		base.OnInspectorGUI();
		
	}
	[MenuItem ("MyMenu/Example_0000_Atlas_01/AtlasMaker")]
	static private void MakeAtlas()
	{
		string spriteDir = Application.dataPath +"/Resources/Example_0000_Atlas_01";
		
		if(!Directory.Exists(spriteDir)){
			Directory.CreateDirectory(spriteDir);
		}
 
		DirectoryInfo rootDirInfo = new DirectoryInfo (Application.dataPath +"/Textures/Example_0000_Atlas_01");
		foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
			foreach (FileInfo pngFile in dirInfo.GetFiles("*.png",SearchOption.AllDirectories)) {
				string allPath = pngFile.FullName;
				string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
				//Sprite sprite = Resources.LoadAssetAtPath<Sprite>(assetPath);//已过时
				Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
				
				GameObject go = new GameObject(sprite.name);
				go.AddComponent<SpriteRenderer>().sprite = sprite;
				 allPath = spriteDir+"/"+sprite.name+".prefab";
				string prefabPath = allPath.Substring(allPath.IndexOf("Assets"));
				PrefabUtility.CreatePrefab(prefabPath,go);
				GameObject.DestroyImmediate(go);
			}
		}	
	}
}
}
